<?php
class contrl_admin extends add_contrl {

 	function chose($url){        $data['sys_info']=$this->show_sys_info($url [1]);
        $data['login_info']=$this->login_info();
 		if($this->level_access($url [0])){ 		switch($url [0]){ 			case("edit"):{ 			 $data['dir']=$this->tree->build_tree("cat",$url[1]); 			 $data['cat']=$this->model->get_info($url[1],"name");
 			 $data['id']=$this->model->get_info($url[1],"id");
             $data['body']=$this->view->view("templates/admin/edit.tpl",$data,true);
              if(!isset($url[2]))$url[2]=1;
             $data['dir_doc']=$this->tree->get_doc($data['id'],$url[2],"","edit");
   			}break;

 			case("create"):{
 			 $data['cat']=$this->model->get_info($url[1],"name");
             $data['id']=$this->model->get_info($url[1],"id");
             if(!isset($url[2]))$url[2]=1;
             $data['dir']=$this->tree->get_doc($data['id'],$url[2],"","create");
             $data['body']=$this->view->view("templates/admin/create.tpl",$data,true);
        	 }break;

 			case("edit_doc"):{             //$data['editor']=$this->model->get_info($url[1],"text","","ns_doc"); future func
             $data['name']=$this->model->get_info($url[1],"name","","ns_doc");
             $data['id']=$url[1];
             $data['option']=$this->tree->get_dir($url[3]);
             $data['dir']=$this->tree->get_doc($url[3],$url[2],$url[1]);
             $data['body']=$this->view->view("templates/admin/doc/edit_doc.tpl",$data,true);
 			}break;

           case("save_edit"):{          	$this->model->save($url[1],$_POST['name'],$_POST['show']);
            header("location: /".ADMIN_DIR);
            }break;

            case("save_edit_d"):{
             $this->model->save_d($url[1],$_POST['edit'],$_POST['name'],$_POST['go'],$_POST['show_me']);
             header("location: /".ADMIN_DIR);
            }break;

            case("save_doc"):{
             $this->model->save_doc($url[1],$_POST['edit'],$_POST['name'],$_POST['show_me']);
             header("location: /".ADMIN_DIR);
            }break;

			case("add"):{			 $data['cat']=$this->model->get_info($url[1],"name");
			 $data['id']=$this->model->get_info($url[1],"id");
   			 $data['body']=$this->view->view("templates/admin/add_del.tpl",$data,true);
             }break;

			case("add_in"):{			 $this->model->add_in($url[1],$_POST['add_in'],$_POST['show'],$_POST['adress'],$_POST['side']);
             header("location: /".ADMIN_DIR);
			}break;

            case("add_r"):{
             $this->model->add_r($url[1],$_POST['add_r'],$_POST['show'],$_POST['adress'],$_POST['side']);
             header("location: /".ADMIN_DIR);
			}break;

			case("del"):{			 $this->model->del_cat($url[1],$url[2]);
			 header("location: /".ADMIN_DIR);
			}break;

			case("info"):{			 $data=$this->model->get_info($url[1],"","","ns_doc");
			 $data['body']=$this->view->view("templates/admin/info/info.tpl",$data [0],true);
			}break;

			case("modules"):{			 $data['modules']=$this->view_modules();			 $data['load_info']="";			 if(isset($url[1]) && $url[1]=="ok"){                $data['load_info']=$this->lang ['ok_load_files'];
			 	}else{                 if( $url[1]=="false")$data['load_info']=$this->lang ['er_load_files'];
			 	}             $data['body']=$this->view->view("templates/admin/modules/modules.tpl",$data ,true);
			}break;

			case("load_files"):{
			 $uploadfile = SITE_DIR.$url[1]."/upload/". basename($_FILES['userfile']['name']);
			 if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {			 	if($url[1]=="modules"){			 	 $this->instal_module($uploadfile);
                 header("location:/".ADMIN_DIR."/modules/ok");
                 }else{                  $this->instal_template($uploadfile);                  header("location:/".ADMIN_DIR."/temp_ld/ok");
                  }
			  }else{			    header("location:/".ADMIN_DIR."/".$url[1]."/false");
               }
				  } break;

			case("mod_options"):{			 $data['tr']=$this->return_info_model($url[1]);
			 $info=$this->model->return_info_model($url[1]);
			 $data['mod_option']=call_user_func_array(array($this->load_module($info[0]['class']),$info[0]['name']."_options"),array_slice($url,0));
             $data['body']=$this->view->view("templates\admin\modules\mod_options\mod_options.tpl",$data ,true);
			}break;

			case("templates"):{
             $data['templates_body']=$this->return_info_today_tpl();
             $data['body']=$this->view->view("templates/admin/templates/templates.tpl",$data,true);
			}break;

			case("side"):{			 $data['templates_body']=$this->return_side_contrl($url[1]);			 $data['body']=$this->view->view("templates/admin/templates/templates.tpl",$data,true);
			}break;

			case("temp_all"):{             $data['templates_body']=$this->return_templates_all();			 $data['body']=$this->view->view("templates/admin/templates/templates.tpl",$data,true);
			}break;

			case("temp_ld"):{			 $data['templates_body']=$this->template_load_del();
			 $data['body']=$this->view->view("templates/admin/templates/templates.tpl",$data,true);
			}break;

			case("security"):{             $data['security_body']=$this->return_user_security();
             $data['body']=$this->view->view("templates/admin/security/security.tpl",$data,true);
			}break;

			case("element"):{			 $data['security_body']=$this->return_element($url[1]);			 $data['body']=$this->view->view("templates/admin/security/security.tpl",$data,true);
			}break;

			case("save_element"):{			 $this->security("POST","",200);             $this->model->save_element($_POST['url'],$_POST['level'],$_POST['name'],$_POST['id_level']);
             header("location: /".ADMIN_DIR."/element/ok");
			}break;

			case("quit_mod_opt"):{			 header("location: /".ADMIN_DIR."/modules/");
			}break;

            case("save_l"):{
             if($_POST['level']>=1){              $this->model->save_level($this->security($_POST['level'],3),$url[1]);
              header("location: /".ADMIN_DIR."/element/ok");
              }else{              	header("location: /".ADMIN_DIR."/element/false");               }

            }break;

            case("quit_adm"):{             session_unset();             session_destroy();             header("location: /".ADMIN_DIR);
            }break;

            case("quit"):header("location: /".ADMIN_DIR."/".$url[1]);break;

            //insert=-=-=-=-=-=-=-=-=-=-=-

            case("edit_user"):{             if(isset($url[1]) && $url[1]!="ok" & $url[1]!="false"){              $data['security_body']=$this->edit_profile($url[1]);
              }else{               $data['security_body']=$this->edit_profile();
              }             $data['body']=$this->view->view("templates/admin/security/security.tpl",$data ,true);
            }break;

            case("s_add_login"):{             $this->security("POST");
             if($this->security($_POST['level'],3)){             	if(isset($url[1])){                 $this->model->save_login($_POST['type'],$_POST['level'],$url[1]);
             	 }else{                 $this->model->save_login($_POST['login'],$_POST['level']);
                 }
                 header("location: /".ADMIN_DIR."/edit_user/ok");             	}else{             	 header("location: /".ADMIN_DIR."/edit_user/false");
             		};
            }break;

            case("s_add_user"):{
             $this->security("POST","",200);
             $this->model->save_user($_POST['name'],$_POST['adress'],md5($_POST['pass']),$_POST['type'],$url[2],$url[1]);
             header("location: /".ADMIN_DIR."/edit_user/ok");
            }break;

            case("s_del_user"):{             $this->model->del_user($url[1]);
              header("location: /".ADMIN_DIR."/edit_user/ok");
            }

            case("s_del_login"):{
             $this->model->del_login($url[1]);
              header("location: /".ADMIN_DIR."/edit_user/ok");
            }

            case("s_edit_login"):{             $data['security_body']=$this->edit_login($url[1]);             $data['body']=$this->view->view("templates/admin/security/security.tpl",$data ,true);
            }break;

            case("tem_side_l"):{              $this->model->save_side_l_un($url[1],$url[2],$url[3]);              header("location: /".ADMIN_DIR."/side/ok");
            }break;

            case("tem_side_d"):{             $this->model->tem_del_side($url[1],$url[2]);
             header("location: /".ADMIN_DIR."/side/ok");
            }break;

            case("tem_add_s"):{             $this->model->save_side($this->security($_POST['name']),$url[1]);
             header("location: /".ADMIN_DIR."/side/ok");
            }break;

            case("save_tmp_inf"):{             $this->security("POST","",200);             $this->model->save_tmp_inf($url[1],$_POST['id'],$_POST['path_file'],$_POST['name'],$_POST['show_me']);
             header("location: /".ADMIN_DIR."/temp_all/ok");
            }break;

            case("tem_del_tem"):{             $this->model->del_templates($url[1]);
             unlink(SITE_DIR."templates/site/tpl_site/".$url[1]);
             header("location: /".ADMIN_DIR."/temp_ld/ok");
            }break;

            case("lang"):{             $class=$this->load_module("lang");             $data['lang_all']=call_user_func(array($class, "show_table_lang"));             $data['body_lang']=$this->view->view("templates/admin/lang_body.tpl",$data,true);             $data['body']=$this->view->view("templates/admin/lang/lang.tpl",$data,true);
            }break;

 		    default:{ 		    $data['body']=$this->tree->build_tree("all");
 		    $data['body']=$this->view->view("templates/admin/catalog/catalog.tpl",$data ,true);
 		    }break;

 		}



 		//END OF SWITCH _CASE--------=-=-=-=-=-=-==-=-==--------------=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-


 	  }else{ 	 	$data['body']=$this->view->view("templates/admin/security/security_not_access.tpl",$data ,true);
 	   }
     return  $data;
 	}

 	function start_admin(){ 	 $this->lang=parse_ini_file(SITE_DIR."/lang/admin/controler/ru.ini",false);     if($this->use_session("access")){     array_shift($this->url); 	 $data=$this->chose($this->url);
     $this->view->view("templates/admin/index.tpl", $data+$this->lang+$this->path);
      }else{       $this->use_session("start");       $data['error_!']=$this->login();
       $this->view->view("templates/admin/login/view_login.tpl", $data+$this->lang+$this->path);
      }
     }
}
?>